package com.example.orderfoodsystem22201310liuziming.controller;

import com.example.orderfoodsystem22201310liuziming.entity.*;
import com.example.orderfoodsystem22201310liuziming.repository.AllOrderRepository;
import com.example.orderfoodsystem22201310liuziming.repository.OrderRepository;
import com.example.orderfoodsystem22201310liuziming.repository.TableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/table")
public class TableChoiceController {
    @Autowired
    private HttpSession httpSession;
    @Autowired
    private TableRepository tableRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private AllOrderRepository allOrderRepository;

    @RequestMapping("/buyer") //买家加载餐桌
    protected String tableChoice() {
        return "buyer/BuyerTableChoice";
    }

    @RequestMapping("/seller")//卖家加载餐桌
    protected String tableCkeck(Model model){
        List<Integer> numlist = Arrays.asList(1,0);
        allorder allorder1 = allOrderRepository.findByTablenumberAndStatusIn(1,numlist);
        if(null != allorder1){
            model.addAttribute("table1", allorder1.getId());
        }
        allorder allorder2 = allOrderRepository.findByTablenumberAndStatusIn(2,numlist);
        if(null != allorder2){
            model.addAttribute("table2", allorder2.getId());
        }
        allorder allorder3 = allOrderRepository.findByTablenumberAndStatusIn(33,numlist);
        if(null != allorder3){
            model.addAttribute("table3", allorder3.getId());
        }
        allorder allorder4 = allOrderRepository.findByTablenumberAndStatusIn(44,numlist);
        if(null != allorder4){
            model.addAttribute("table4", allorder4.getId());
        }
        return "/seller/SellerTableCheck";
    }

//    @RequestMapping("/seller2")//卖家加载餐桌
//    protected String tableCkeck2(Model model) {
//        List<allorder> allorderList = allOrderRepository.findAllByStatusIn(Arrays.asList(1,0));
//        List<foodtable> foodtableList = tableRepository.findAllByStateIn(Arrays.asList(1,-1));
//        Map<Integer, allorder> integerbuyerMap = new HashMap<>();
//        for (foodtable f: foodtableList
//        ) {
//            allorder b = new allorder();
//            b.setTablenumber(f.getId());
//            b.setId(-1);
//            integerbuyerMap.put(f.getId(), b);
//        }
//        for (allorder a: allorderList
//        ) {
//            integerbuyerMap.put(a.getTablenumber(), a);
//        }
//        model.addAttribute("tableList", integerbuyerMap);
//        return  "seller/SellerTableAll";
//    }

    @RequestMapping("/onuse/{Id}") //餐桌占用
    protected String tableUse(Model model, @PathVariable("Id") Integer id) {
        //----------------------更新状态-----------------------------------------
        Optional<foodtable> foodtableOptional = tableRepository.findById(id);
        if (foodtableOptional.isEmpty()) {
            System.out.println("不存在记录");
        }
        foodtable onuse = foodtableOptional.get();
        onuse.setState(-1);
        tableRepository.save(onuse);
        httpSession.setAttribute("tableID", id);
        return "redirect:/buyer/menulist";
    }



    @RequestMapping("/nouse/{Id}")   //餐桌复用
    protected String tableNoUse(@PathVariable("Id") Integer id){
        Optional<foodtable> foodtableOptional = tableRepository.findById(id);
        if (foodtableOptional.isEmpty()) {
            System.out.println("不存在记录");
        }else {
            foodtable nouse = foodtableOptional.get();
            nouse.setState(1);
            tableRepository.save(nouse);

            String username = (String)httpSession.getAttribute("aaaa");
            allorder allorderOptional = allOrderRepository.findByUsername(username);
            allorderOptional.setStatus(-1);
            allOrderRepository.save(allorderOptional);

            httpSession.removeAttribute("finish");
            httpSession.removeAttribute("aaaa");
        }
        return "redirect:/";
    }


    @RequestMapping(value = "/tableJson")
    @ResponseBody
    protected Iterable<foodtable>tableJson(){
        return tableRepository.findAll();
    }
}
